﻿using Bangle.SerialPort;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;

namespace Bangle.SerialPort.Abp
{
    public class SerialPortAbpModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            context.Services.AddSerialPort(configuration);
        }

        public override void PostConfigureServices(ServiceConfigurationContext context)
        {
            AsyncHelper.RunSync(() =>
            {
                var provider = context.Services.BuildServiceProvider();
                var factory = provider.GetService<SerialPortFactory>();
                factory.OpenAll(provider);
                return Task.CompletedTask;
            });
        }
    }
}
